﻿using Slex;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Test.Scenes {
    public partial class DynamicBinding : UserControl {
        public ObservableCollection<Product> data1 = new ObservableCollection<Product>();
        public ObservableCollection<ExtensibleType<Product2>> data2 = new ObservableCollection<ExtensibleType<Product2>>();

        public DynamicBinding() {
            InitializeComponent();
            productsGrid.ItemsSource = data2;
            lb.ItemsSource = data2;
            Loaded += DynamicBinding_Loaded;
        }

        void DynamicBinding_Loaded(object sender, RoutedEventArgs e) {
            ExtensibleType<Product2>.AddProperty("Date", typeof(DateTime));
            ExtensibleType<Product2>.AddProperty("Name2", typeof(string));
            ExtensibleType<Product2>.AddProperty("Name", typeof(Byte));//故意设置被扩展类相同名称的属性
            ExtensibleType<Product2>.AddProperty("Prod", typeof(Product2));
            var pd2 = new Product2();
            var p3 = new ExtensibleType<Product2>(pd2);
            //ExtensibleType<Product2>.AddProperty("Prod", typeof(Product2));
            p3.SetPropertyValue("Date", DateTime.Now);
            p3.SetPropertyValue("Name2", "aaa");
            p3.SetPropertyValue("Name", (byte)111);
            p3.SetPropertyValue("Price", 253);
            //MessageBox.Show(p3.GetPropertyValue("Price").ToString());
            p3.SetPropertyValue("Price", 253);
            p3.SetPropertyValue("Price", 353);
            var p4 = new ExtensibleType<Product2>(new Product2());
            p4.SetPropertyValue("Date", DateTime.Now.AddDays(new Random().Next(100)));
            p4.SetPropertyValue("Name2", "bbb");
            p4.SetPropertyValue("Name", (byte)211);
            p4.SetPropertyValue("Price", 553);
            //MessageBox.Show(p3.GetPropertyValue("Price").ToString());
            data2.Add(p3);
            data2.Add(p4);
            var x = p3.GetType().GetProperties();
            var x2 = typeof(ExtensibleType<Product2>).GetProperties();
            var sb = new StringBuilder();
            foreach (var item in x) {
                sb.AppendLine(string.Format("{0},{1}", item.Name, item.GetValue(p3, null)));
            }
            MessageBox.Show(p3.GetPropertyValue("Date").Year.ToString());
            MessageBox.Show(p3.GetPropertyValue("Date").Month.ToString());
            MessageBox.Show(p3.GetPropertyValue("Date").Day.ToString());
            p3.SetPropertyValue("Prod", new Product2 { Name="kaiyuan"});
            MessageBox.Show(p3.GetPropertyValue("Prod").Name);
            //p3.GetPropertyValue("Prod").Name += "222";
            MessageBox.Show(p3.GetPropertyValue("Prod").Name);
            MessageBox.Show(p3.GetPropertyValue("Prod").ToString());
            p3.Entity.Name = "bbb+ccc";
        }
    }
    public class Product:Slex.ExtensibleType<Product> {
        public string Name { get; set; }
        public int Price { get; set; }
        
    }
    public class Product2 {
        public string Name { get; set; }
        public int Price { get; set; }
        public override string ToString() {
            return string.Format("{0}.{1}", Name, Price);
        }
    }
}
